#!/bin/bash

source /home/shell/util.sh

function install () {

    curl -sL https://repo.huaweicloud.com/rabbitmq-server/|grep -Po "v\d+.\d+.\d+"|uniq|perl -pe "s/v(.*)/\1/g"|tail -10

    while [ true ]; do
        printInfo "请选择rabbitmq列表"
        read -p "请选择版本：" version;
        if [[ -z "${version}" ]]; then
            printFail "版本不能为空"
        else
            break;
        fi
    done
    
    if [ "$(ostype)" == "Ubuntu" ]; then
        wget -O- https://packages.erlang-solutions.com/ubuntu/erlang_solutions.asc | sudo apt-key add -
        echo "deb https://packages.erlang-solutions.com/ubuntu focal contrib" | sudo tee /etc/apt/sources.list.d/rabbitmq.list

        sudo apt update
        sudo apt-get install -y erlang-base \
                        erlang-asn1 erlang-crypto erlang-eldap erlang-ftp erlang-inets \
                        erlang-mnesia erlang-os-mon erlang-parsetools erlang-public-key \
                        erlang-runtime-tools erlang-snmp erlang-ssl \
                        erlang-syntax-tools erlang-tftp erlang-tools erlang-xmerl \
                        curl gnupg debian-keyring debian-archive-keyring apt-transport-https

        dpkg-install-fromurl https://repo.huaweicloud.com/rabbitmq-server/v${version}/rabbitmq-server_${version}-1_all.deb
        ## 安装后清理
        rm -rf /etc/apt/sources.list.d/rabbitmq.list
        sudo apt update -y
    else
        curl -s https://packagecloud.io/install/repositories/rabbitmq/erlang/script.rpm.sh | sudo bash
        sudo yum clean all
        sudo yum -y makecache
        yum install -y erlang
        rpm -ivh https://repo.huaweicloud.com/rabbitmq-server/v${version}/rabbitmq-server-${version}-1.el8.noarch.rpm
    fi
    
    # 安装插件
    version=`rabbitmqctl version`
    plugin_dir=/usr/lib/rabbitmq/lib/rabbitmq_server-${version}/plugins

    curl -sL https://hub.fastgit.org/rabbitmq/rabbitmq-delayed-message-exchange/releases|grep "ez</span>"|perl -pe "s/.*<span.*?>rabbitmq_delayed_message_exchange-(.*?).ez<\/span>/\1/g"
    while [ true ]; do
        printInfo "rabbitmq_delayed_message_exchange版本列表"
        read -p "请选择版本：" delayed_message_version;
        if [[ -z "${delayed_message_version}" ]]; then
            printFail "版本不能为空"
        else
            break;
        fi
    done
    download $plugin_dir/rabbitmq_delayed_message_exchange.ez https://hub.fastgit.org/rabbitmq/rabbitmq-delayed-message-exchange/releases/download/${delayed_message_version}/rabbitmq_delayed_message_exchange-${delayed_message_version}.ez

}

function config () {
    read -p "新增管理员名[admin]：" user_name;
    user_name=${user_name:="admin"}
    read -p "新增管理员密码[123456]：" user_passwd;
    user_passwd=${user_passwd:="123456"}
    # rabbitmq管理插件
    rabbitmq-plugins enable rabbitmq_management
    # 延迟队列插件
    rabbitmq-plugins enable rabbitmq_delayed_message_exchange 

    rabbitmq-plugins enable rabbitmq_shovel rabbitmq_shovel_management

    ## 启动并设置为开机自启动
    systemctl enable --now rabbitmq-server
    # 创建管理员账号并赋权
    rabbitmqctl add_user ${user_name} ${user_passwd}
    rabbitmqctl set_permissions -p / ${user_name} ".*" ".*" ".*"
    rabbitmqctl set_user_tags ${user_name} administrator
    # 添加防火墙端口，管理页端口和mq端口
    firewall-addport 15672
    firewall-addport 5672
    # 安装成功显示的信息
    printInfo "=======================安装信息======================="
    printInfo "rabbitmq安装成功，后台管理地址为：http://$host_addr:15672"
    printInfo "用户名: ${user_name}"
    printInfo "密码: ${user_passwd}"
    printInfo "=======================安装信息======================="
}



install && config